package com.android.hz.czc.controller;


import com.android.hz.czc.entity.User;
import com.android.hz.czc.resultvue.Result;
import com.android.hz.czc.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hly
 * @since 2019-01-02
 */
//@Api(tags = "user",description = "密码相关")

@Validated
@Slf4j
@Api(value = "用户相关Controller", description = "用户相关", tags = { "user" })
@RestController
@RequestMapping("/czc/user")
public class UserController extends BaseController{
    @Autowired
    private IUserService userService;

    @ApiOperation(value="注册用户", notes="根据用户名密码注册用户，别名可以指定也可以不填，默认为用户名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",  dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "alias", value = "别名",dataType = "String",paramType="query")
    })
    @PostMapping(value = "/register")
    public Result register(
            @NotBlank(message = "用户名不能为空") @RequestParam(value = "username") String username,
            @NotBlank(message = "密码不能为空") @RequestParam(value = "password") String password,
            @RequestParam(value = "alias",required = false) String alias) {
        if (StringUtils.isBlank(alias)){alias = username;}
        log.info("user:{}",new User(username,alias,password));
        return userService.IRegister(new User(username,alias,password));
    }

    @ApiOperation(value="登陆",notes = "根据用户名密码进行登陆验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "/login")
    public Result login(
            @RequestParam(value = "username") String username,
            @RequestParam(value = "password") String password) {
        return userService.ILogin(new User(username,password));
    }

}
